package com.atguigu.day10;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple1;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.ScalarFunction;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.*;

public class Flink04_UDF_TableFun {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.读取元素得到DataStream
//        DataStreamSource<WaterSensor> waterSensorDataStreamSource = env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
//                new WaterSensor("sensor_1", 2000L, 20),
//                new WaterSensor("sensor_2", 3000L, 30),
//                new WaterSensor("sensor_1", 4000L, 40),
//                new WaterSensor("sensor_1", 5000L, 50),
//                new WaterSensor("sensor_2", 6000L, 60));

        SingleOutputStreamOperator<WaterSensor> waterSensorDataStreamSource = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                });

        //3.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //4.将流转为表
        Table table = tableEnv.fromDataStream(waterSensorDataStreamSource);

        //不注册直接使用
   /*     table
                .joinLateral(call(MyUDTF.class,$("id")))
//                .select($("id"),$("f0"))
                .select($("id"),$("word"))
                .execute().print();*/


        //先注册再使用
        tableEnv.createTemporarySystemFunction("MyExplor", MyUDTF.class);

        /*table
                .leftOuterJoinLateral(call("MyExplor", $("id")))
                .select($("id"),$("word"))
                .execute().print();*/

        //sql
     /*   tableEnv.executeSql("select " +
                "id," +
                "word " +
                "from "+table+
                " join lateral table(MyExplor(id)) on true").print();*/
     tableEnv.executeSql("select " +
             "id," +
             "word " +
             "from "+table+", lateral table(MyExplor(id))").print();

    }
    //自定义一个表函数，一进多出，根据id按照下划线切分出多个数据
//    public static class MyUDTF extends TableFunction<Tuple1<String>>{
    @FunctionHint(output = @DataTypeHint("ROW<word String>"))
    public static class MyUDTF extends TableFunction<Row>{
        public void eval(String value){
            String[] split = value.split("_");
            for (String s : split) {
//                collect(Tuple1.of(s));
                collect(Row.of(s));
            }
        }
    }

}
